package com.yenlien.traceyun.control.controller;

import com.alibaba.fastjson.JSONObject;

import com.yenlien.traceyun.control.config.Log;
import com.yenlien.traceyun.control.entity.Word;
import com.yenlien.traceyun.control.service.WordService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/word")
public class WordController extends BaseController {

    @Autowired
    private WordService wordService;

    /**
     * 新增意见
     *
     * @param params
     * @return
     */
    @PostMapping("/save")
    @Log(operation = "意见反馈", profession = "新增反馈")
    public Object save(@RequestBody JSONObject params) {
        Word word = params.getJSONObject("word").toJavaObject(Word.class);
        wordService.save(word);
        return success("success");
    }

    /**
     * 查询意见
     *
     * @param params
     * @return
     */
    @PostMapping("/inquire")
    public Object inquire(@RequestBody JSONObject params) {
        Integer page = params.getInteger("page");
        if (page > 0) {
            Integer page1 = page - 1;
            return buildPage(wordService.select(page1));
        } else {
            return fail("页面加载错误");
        }
    }

    /**
     * 搜索栏(模糊查询)
     *
     * @param params
     * @return
     */
    @PostMapping("/search")
    @Log(operation = "意见反馈", profession = "搜索反馈")
    public Object search(@RequestBody JSONObject params) {
        String caption = params.getString("caption");
        String captions = "%" + caption + "%";
        if (!StringUtils.isEmpty(caption)) {
            return success(wordService.search(captions));
        } else {
            return fail("信息未存在");
        }
    }

    /**
     * 根据id查询意见
     *
     * @param params
     * @return
     */
    @PostMapping("/selectId")
    @Log(operation = "意见反馈", profession = "查询反馈")
    public Object selectId(@RequestBody JSONObject params) {
        String id = params.getString("id");
        if (id.length() > 0) {
            return success(wordService.selectId(id));
        } else {
            return fail("意见不存在");
        }
    }

}
